﻿<?php
class UserMapper extends Mapper{

    protected $tableName = 'users';
    
    public function getCurrentUser() {
    
        @session_start();
        //$_SESSION['userID'] = '';
        if (@!$_SESSION['userID']) {
            $currentUser = new User();
            $currentUser->id = '0';
            $currentUser->fName = 'Дорогой Гость';
        } else {
            $data = $this->dao->fetch($_SESSION['userID']);
            if(!is_array($data)) return false;
            $currentUser = new User($data[0]);
        }
        return $currentUser;
    }
    
    public function logOut($user) {
    
        $_SESSION['userID'] = '';
        $user->id = '0';
        $user->userName = '';
        $user->fName = 'Дорогой Гость';
        $user->lName = '';
    }
    
    public function regUser($data) {
    
        $regUser = new User($data);
        
        if ($data['userName'] && $data['password'] && $data['passwordCheck'] && $data['fName'] && $data['lName']) {
                
            if ($this->dao->fetch($data['userName'], 'userName')) {
                $regUser->notice = 'Автор с таким логином уже есть. Выбери другой.';
                return $regUser;
            }
            
            if ($data['password'] == $data['passwordCheck']) {
                $kArray = array();
                $kArray['userName'] = $data['userName'];
                $kArray['password'] = $data['password'];
                $kArray['fName'] = $data['fName'];
                $kArray['lName'] = $data['lName'];
                
                $this->dao->addEntry($kArray);
            } else {
                $regUser->notice = 'Твой пароль не прошел проверку.';                
            }
        } else {
            $regUser->notice = 'Ты заполнил не все поля.';
        }
        return $regUser;
    }
    
    public function authUser($data, $user){
    
        $check = $this->dao->fetch($data['userName'], 'userName');
        
        if ($check) {
            $userCheck = $check[0];
            if ($userCheck['password'] == $data['password']) {
                
                $user->id = $userCheck['id'];
                $user->userName = $userCheck['userName'];
                $user->fName = $userCheck['fName'];
                $user->lName = $userCheck['lName'];
                
                $_SESSION['userID'] = $user->id;
                return true;
            }
        }
        
        return false;
    }
    
    public function getAllUsers() {

        $data = $this->dao->fetch(1,1);
        $users = array();
        
        foreach($data as $row) {
            if($row['userName'] == 'admin') continue;
            $user = new User($row);
            $users[] = $user;
        } 
        return $users;
    }
    
    public function getFullUserName($data) {
        if($data) {
            $authorsID = array();
            if(is_array($data)) {
                foreach($data as $v) $authorsID[] = $v->userID;
                $authorsID = array_unique($authorsID);
            } else $authorsID[] = $data->userID;
    
            $result = $this->dao->fetchByArray( $authorsID, 'id, lName, fName' );
            $authorsName = array();
    
            foreach ($result as $v) $authorsName[$v['id']] = $v['fName'] . ' ' . $v['lName'];
                    
            if(is_array($data)) { 
                foreach($data as $k => $v) {
                $data[$k]->userFullName = $authorsName[$v->userID];
                }
            } else $data->userFullName = $authorsName[$data->userID];
        }
    }
    
    public function getKarma($data) {
        if($data) {
            $authorsID = array();
            if(is_array($data)) {
                foreach($data as $v) $authorsID[] = $v->userID;
                $authorsID = array_unique($authorsID);
            } else $authorsID[] = $data->userID;
    
            $result = $this->dao->fetchByArray( $authorsID, 'id, karma' );
            $authorsKarma = array();
    
            foreach ($result as $v) $authorsKarma[$v['id']] = $v['karma'];
                    
            if(is_array($data)) { 
                foreach($data as $k => $v) {
                $data[$k]->karma = $authorsKarma[$v->userID]?$authorsKarma[$v->userID]:0;
                }
            } else $data->karma = $authorsKarma[$data->userID]?$authorsKarma[$data->userID]:0;
        }
    }
    
    public function addAndGetKarma($data, $curUserID) {
        
        $delta = 10;
        
        $result = $this->dao->fetch($data['userID'],'id','karma, karmaTime'); 
        $time = time();
        $deltaTime = $time - $result[0]['karmaTime'];
        if(($deltaTime > $delta) && !($curUserID == 0)) {
            $karma = $result[0]['karma'] + 1;
            $this->dao->update(array('id'=>$data['userID'], 'karma'=>$karma, 'karmaTime'=>$time));
        }
        else $karma = $result[0]['karma'];
        
        return $karma;
    }
    
    public function reductionKarma() {
        if ($_SERVER['REMOTE_ADDR'] == '127.0.0.1') {
            //$result = $this->dao->fetch(1,1,'id, karma');
            //foreach($result as $v) {
                //$v['karma'] = $v['karma'] - 0.01;
                //$this->dao->update($v);
            //}
            $this->dao->updateKarma();
        }
    }
}
    
    


?>